package mall.repository;

import mall.core.jdbc.QueryWrapper;
import mall.core.jdbc.Repository;
import mall.entity.User;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.ArrayList;
import java.util.List;

public class UserRepository extends Repository {

    public List<User> selectList() {
        return jdbcTemplate.query("select * from user", new BeanPropertyRowMapper<>(User.class));
    }

    public List<User> selectList(User user) {
        QueryWrapper queryWrapper = new QueryWrapper("user")
                .eq(user.getId() != null, "id", user.getId())
                .like(user.getName() != null, "name", user.getName());

        return jdbcTemplate.query(queryWrapper.getSql(), new BeanPropertyRowMapper<>(User.class), queryWrapper.getParams());
    }

    public Integer count(User user) {
        StringBuilder sb = new StringBuilder("select count(*) from user where 1=1 ");
        List<Object> params = new ArrayList<>();

        if (user.getId() != null) {
            sb.append(" and id = ?");
            params.add(user.getId());
        }
        if (user.getName() != null) {
            sb.append(" and name like ?");
            params.add("%" + user.getName() + "%");
        }

        return jdbcTemplate.queryForObject(sb.toString(), Integer.class, params.toArray());
    }

    public List<User> selectList(User user, Integer offset, Integer size) {
        QueryWrapper queryWrapper = new QueryWrapper("user")
                .eq(user.getId() != null, "id", user.getId())
                .like(user.getName() != null, "name", user.getName())
                .limit(offset, size);

        return jdbcTemplate.query(queryWrapper.getSql(), new BeanPropertyRowMapper<>(User.class), queryWrapper.getParams());
    }


    public int insert(User user) {
        String sql = "insert into user(name, username, password, role) values(?, ?, ?, ?)";
        return jdbcTemplate.update(sql, user.getName(), user.getUsername(), user.getPassword(), user.getRole());
    }

    public int update(User user) {
        String sql = "update user set name = ?, username = ?, password = ?,phone = ?, address = ? where id = ?";
        return jdbcTemplate.update(sql, user.getName(), user.getUsername(), user.getPassword(), user.getPhone(), user.getAddress(), user.getId());
    }

    public int deleteById(int id) {
        String sql = "delete from user where id = ?";
        return jdbcTemplate.update(sql, id);
    }


    public User selectById(int id) {
        String sql = "select * from user where id = ?";
        List<User> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class), id);
        return list.isEmpty() ? null : list.get(0);
    }


    public User selectByUsernamePassword(String username, String password) {
        String sql = "select * from user where username = ? and password = ?";
        List<User> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class), username, password);
        return list.isEmpty() ? null : list.get(0);
    }

    public User selectByUsername(String username) {
        String sql = "select * from user where username = ?";
        List<User> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class), username);
        return list.isEmpty() ? null : list.get(0);
    }
}
